package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.EvaluateMapper;
import com.chixing.mapper.HotelMapper;
import com.chixing.mapper.MyorderMapper;
import com.chixing.pojo.*;
import com.chixing.service.EvaluateService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private MyorderMapper myorderMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public ServerResponse getByConditions(String hotalName, Integer pageNum) {
        Page page = PageHelper.startPage(pageNum,3);
        HotelExample hotelExample = new HotelExample();
        hotelExample.createCriteria().andHotelNameEqualTo(hotalName);
        Hotel hotel = hotelMapper.selectByExample(hotelExample).get(0);
        MyorderExample orderExample = new MyorderExample();
        orderExample.createCriteria().andHotelIdEqualTo(hotel.getHotelId());
        List<Myorder> orderList = myorderMapper.selectByExample(orderExample);
        EvaluateExample evaluateExample = new EvaluateExample();
        List<EvaluateVoOrder> volist = new ArrayList<>();
        EvaluateVoOrder eoVo = new EvaluateVoOrder();
        for (int i=0;i<orderList.size();i++){
            evaluateExample.createCriteria().andOrderIdEqualTo(orderList.get(i).getOrderId());
            Evaluate evaluate = evaluateMapper.selectByExample(evaluateExample).get(0);
            eoVo.setMyorder(orderList.get(i));
            eoVo.setEvaluate(evaluate);
        }
        volist.add(eoVo);
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(volist);
        if (orderList.size()>0)
            return ServerResponse.getSuccess(pageInfo);
        return ServerResponse.getFail(null);

    }

    @Override
    public ServerResponse getBySave(Integer cusId, String houseName) {
        HotelExample hotelExample = new HotelExample();
        hotelExample.createCriteria().andHotelNameEqualTo(houseName);
        Hotel hotel = hotelMapper.selectByExample(hotelExample).get(0);
        MyorderExample orderExample = new MyorderExample();
        orderExample.createCriteria().andCusIdEqualTo(cusId).andHotelIdEqualTo(hotel.getHotelId());
        Myorder order = myorderMapper.selectByExample(orderExample).get(0);
        EvaluateExample evaluateExample = new EvaluateExample();
        evaluateExample.createCriteria().andOrderIdEqualTo(order.getOrderId());
        Evaluate evaluate = evaluateMapper.selectByExample(evaluateExample).get(0);
        if (evaluate !=null)
            return ServerResponse.getSuccess(evaluate);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByHoutlId(Integer id, Integer pageNum) {
        Page page = PageHelper.startPage(pageNum,5);
        Hotel hotel = hotelMapper.selectByPrimaryKey(id);
        MyorderExample orderExample = new MyorderExample();
        orderExample.createCriteria().andHotelIdEqualTo(hotel.getHotelId());
        List<Myorder> orderList = myorderMapper.selectByExample(orderExample);
        EvaluateExample evaluateExample = new EvaluateExample();
        Evaluate evaluate;
        Customer customer;
        List<EvaluateVoOrder> volist = new ArrayList<>();
        EvaluateVoOrder eoVo = new EvaluateVoOrder();
        for (Myorder myorder:orderList){
            evaluateExample.createCriteria().andOrderIdEqualTo(myorder.getOrderId());
            List<Evaluate> evaluateList = evaluateMapper.selectByExampleWithBLOBs(evaluateExample);
            customer = customerMapper.selectByPrimaryKey(myorder.getCusId());
            if (evaluateList !=null && evaluateList.size()>0){
                evaluate = evaluateList.get(0);
                eoVo.setEvaluate(evaluate);
                eoVo.setMyorder(myorder);
                eoVo.setCustomer(customer);
                volist.add(eoVo);
            }
        }

        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(volist);
        if (orderList.size()>0)
            return ServerResponse.getSuccess(pageInfo);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByCusId(Integer cusId) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andCusIdEqualTo(cusId);
        List<Myorder> myorderList = myorderMapper.selectByExample(myorderExample);
        Evaluate evaluate;
        Hotel hotel;
        List<EvaluateVoHotel> volist = new ArrayList<>();
        EvaluateVoHotel eoVo = new EvaluateVoHotel();
        if (myorderList !=null && myorderList.size()>0){
            for (Myorder myorder:myorderList){
                EvaluateExample evaluateExample = new EvaluateExample();
                evaluateExample.createCriteria().andOrderIdEqualTo(myorder.getOrderId());
                List<Evaluate> evaluateList = evaluateMapper.selectByExampleWithBLOBs(evaluateExample);
                if (evaluateList !=null && evaluateList.size()>0){
                     evaluate = evaluateList.get(0);
                    hotel = hotelMapper.selectByPrimaryKey(evaluate.getHotelId());
                    eoVo.setHotel(hotel);
                    eoVo.setEvaluate(evaluate);
                    volist.add(eoVo);
                }

            }
            return ServerResponse.getSuccess(volist);
        }else {
            return ServerResponse.getFail(null);
        }

    }

    @Override
    public ServerResponse save(Evaluate evaluate) {
        int rows=evaluateMapper.insert(evaluate);
        if (rows>0)
           return ServerResponse.saveSuccess(rows);
        return ServerResponse.saveFail(null);
    }

}
